import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;

/**
 * Created with IntelliJ IDEA.
 * Description:
 */
  //线程池的模拟实现
    class MyThreadPool {
        private final int corePoolSize; //核心线程数
    private BlockingQueue<Runnable> workQueue = new LinkedBlockingQueue<>(); //任务队列
        public MyThreadPool(int corePoolSize) {
            this.corePoolSize = corePoolSize;
            for (int i = 0; i < corePoolSize; i++) {
                Thread thread =new Thread(()-> {
                    while (true){

                        try {
                            Runnable task = workQueue.take();
                            task.run();
                        } catch (InterruptedException e) {
                            throw new RuntimeException(e);
                        }
                    }
                });
                thread.start();
            }

        }
        public void submit(Runnable task) throws InterruptedException {
            workQueue.put(task);
        }
    }
public class Demo2 {
    public static void main(String[] args) throws InterruptedException {
        MyThreadPool threadPool = new MyThreadPool(2);
        for (int i = 0; i < 10; i++) {
            int id = i;
            threadPool.submit(() -> {
                System.out.println(Thread.currentThread().getName() + " running task " + id);
    });
}
    }
}
